using ERP_SaaS_WinForm.生产计划_派单_车间.ProductPlan;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace ERP_SaaS_WinForm.生产计划_派单_车间
{
    public partial class ProductPlanForm : CCSkinMain
    {
        private List<Product> productList = new List<Product>();
        private List<SalesOrder> salesOrderList = new List<SalesOrder>();

        public ProductPlanForm()
        {
            InitializeComponent();
            InitializeForm();
        }

        private async void InitializeForm()
        {
            this.Text = "生产计划管理";
            this.WindowState = FormWindowState.Maximized;

            // 加载初始数据
            await LoadProductData();
            await LoadSalesOrderData();
        }

        /// <summary>
        /// 加载产品数据
        /// </summary>
        private async Task LoadProductData()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync("http://localhost:5281/api/Plan/GetProduct");
                    response.EnsureSuccessStatusCode();
                    var jsonResponse = await response.Content.ReadAsStringAsync();
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);
                    
                    if (apiResponse?.code == 200 && apiResponse.data != null)
                    {
                        // 将PlanData转换为Product
                        productList = apiResponse.data.Select(p => new Product
                        {
                            ProductId = p.productId,
                            ProductCode = p.planCode, // 使用planCode作为产品编码
                            ProductName = p.productName,
                            Specification = p.specification,
                            UnitName = p.unitName
                        }).ToList();
                        ShowAlert($"成功加载 {productList.Count} 个产品信息", "提示");
                    }
                    else
                    {
                        ShowAlert($"加载产品数据失败: {apiResponse?.msg}", "错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowAlert($"加载产品数据异常: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 加载销售订单数据
        /// </summary>
        private async Task LoadSalesOrderData()
        {
            try
            {
                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync("http://localhost:5054/api/SaleOrder/GetSaleOrder");
                    response.EnsureSuccessStatusCode();
                    var jsonResponse = await response.Content.ReadAsStringAsync();
                    var apiResponse = JsonConvert.DeserializeObject<ApiResponse>(jsonResponse);
                    
                    if (apiResponse?.code == 200 && apiResponse.data != null)
                    {
                        // 将PlanData转换为SalesOrder（这里需要根据实际的API响应结构调整）
                        // 暂时使用空列表，因为SalesOrder API可能使用不同的数据结构
                        salesOrderList = new List<SalesOrder>();
                        ShowAlert("销售订单数据加载功能待完善", "提示");
                        ShowAlert($"成功加载 {salesOrderList.Count} 个销售订单信息", "提示");
                    }
                    else
                    {
                        ShowAlert($"加载销售订单数据失败: {apiResponse?.msg}", "错误");
                    }
                }
            }
            catch (Exception ex)
            {
                ShowAlert($"加载销售订单数据异常: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 显示提示信息
        /// </summary>
        private void ShowAlert(string message, string caption)
        {
            MessageBox.Show(message, caption, MessageBoxButtons.OK, 
                caption == "成功" ? MessageBoxIcon.Information 
                : caption == "提示" ? MessageBoxIcon.Information 
                : MessageBoxIcon.Error);
        }

        /// <summary>
        /// 点击成品名称 - 显示产品详细信息
        /// </summary>
        private void lblProductName_Click(object sender, EventArgs e)
        {
            try
            {
                if (productList == null || productList.Count == 0)
                {
                    ShowAlert("暂无产品数据，请先加载产品信息", "提示");
                    return;
                }

                // 创建产品信息显示窗体
                var productInfoForm = new ProductInfoForm(productList);
                productInfoForm.ShowDialog();
            }
            catch (Exception ex)
            {
                ShowAlert($"显示产品信息失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 点击订单编号 - 显示订单详细信息
        /// </summary>
        private void lblOrderNumber_Click(object sender, EventArgs e)
        {
            try
            {
                if (salesOrderList == null || salesOrderList.Count == 0)
                {
                    ShowAlert("暂无订单数据，请先加载订单信息", "提示");
                    return;
                }

                // 创建订单信息显示窗体
                var orderInfoForm = new OrderInfoForm(salesOrderList);
                orderInfoForm.ShowDialog();
            }
            catch (Exception ex)
            {
                ShowAlert($"显示订单信息失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 刷新数据按钮
        /// </summary>
        private async void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                ShowAlert("正在刷新数据...", "提示");
                await LoadProductData();
                await LoadSalesOrderData();
                ShowAlert("数据刷新完成", "成功");
            }
            catch (Exception ex)
            {
                ShowAlert($"刷新数据失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 添加生产计划按钮
        /// </summary>
        private void btnAddPlan_Click(object sender, EventArgs e)
        {
            try
            {
                ShowAlert("正在打开添加生产计划窗口...", "提示");
                // TODO: 打开添加生产计划窗体
                // var addPlanForm = new AddPlanForm();
                // addPlanForm.ShowDialog();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开添加生产计划窗口失败: {ex.Message}", "错误");
            }
        }

        /// <summary>
        /// 查询生产计划按钮
        /// </summary>
        private void btnSearchPlan_Click(object sender, EventArgs e)
        {
            try
            {
                ShowAlert("正在打开生产计划查询窗口...", "提示");
                // 打开现有的生产计划查询窗体
                var planMainForm = new ProductPlanMain();
                planMainForm.Show();
            }
            catch (Exception ex)
            {
                ShowAlert($"打开生产计划查询窗口失败: {ex.Message}", "错误");
            }
        }
    }
} 